import React, { useEffect, useState,useRef } from 'react'


export const useCount = (init=0) => {
    const [count,setCount] = useState(init)
    const interval = useRef(null)

    const start = () => {
        interval.current = setInterval(() => {
            setCount(prev => prev + 1)
          },1000)
    }

    const stop = () => {
        clearInterval(interval.current)
    }


    useEffect(() => {
        start()
  
      return () => clearInterval(interval.current)
    },[])
  
    return {count,stop,start}
  
  }